package com.jhgk.jhcee.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.jhgk.jhcee.common.domain.TableListModel;
import com.jhgk.jhcee.common.enums.BasisResultEnum;
import com.jhgk.jhcee.common.resultful.BasisResultful;
import com.jhgk.jhcee.data.auth.entity.SysPermissionInfo;
import com.jhgk.jhcee.data.auth.entity.SysUserInfo;
import com.jhgk.jhcee.data.auth.entity.SysUserRoleRelation;
import com.jhgk.jhcee.data.auth.mapper.SysPermissionInfoMapper;
import com.jhgk.jhcee.data.auth.mapper.SysUserRoleRelationMapper;
import com.jhgk.jhcee.system.domain.model.SysPermissionInfoModel;
import com.jhgk.jhcee.system.service.SysPermissionInfoService;
import com.jhgk.jhcee.system.service.SysUserInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Description:
 * @Author: lemon_xzp
 * @Date: 2021/9/22 16:27
 */
@Service(value = "SysPermissionInfoServiceImpl")
public class SysPermissionInfoServiceImpl extends ServiceImpl<SysPermissionInfoMapper, SysPermissionInfo> implements SysPermissionInfoService {

    @Resource(name = "SysUserInfoServiceImpl")
    SysUserInfoService sysUserInfoService;

    @Autowired
    SysUserRoleRelationMapper sysUserRoleRelationMapper;

    @Autowired
    SysPermissionInfoMapper sysPermissionInfoMapper;

    @Override
    public BasisResultful<Object> getPermissionList() {
        BasisResultful<SysUserInfo> user = sysUserInfoService.getCurrentUser();
        if (user.getCode() != BasisResultEnum.SUCCESS.getCode()) {
            return BasisResultful.fail(BasisResultEnum.UNAUTHORIZED);
        }
        QueryWrapper<SysUserRoleRelation> userWrapper = new QueryWrapper<>();
        userWrapper.eq("user_id", user.getResult().getUserId());
        List<SysUserRoleRelation> sysUserRoleRelations = sysUserRoleRelationMapper.selectList(userWrapper);
        List<SysPermissionInfo> sysPermissionInfos = sysPermissionInfoMapper.selectPermissionList(sysUserRoleRelations.stream().map(SysUserRoleRelation::getRoleId).collect(Collectors.toList()));
        List<Long> permissionList = new ArrayList<>();
        if (!sysPermissionInfos.isEmpty()) {
            permissionList = sysPermissionInfos.stream().map(SysPermissionInfo::getPermissionId).collect(Collectors.toList());
        }
        return BasisResultful.success(permissionList);
    }

    @Override
    public BasisResultful<Object> getPermissionAllList(SysPermissionInfoModel sysPermissionInfoModel) {
        if (null == sysPermissionInfoModel) {
            return BasisResultful.fail(BasisResultEnum.PARAM_IS_NULL);
        }
        PageHelper.startPage(sysPermissionInfoModel.getPage(), sysPermissionInfoModel.getPageSize());
        List<SysPermissionInfo> sysPermissionInfos = sysPermissionInfoMapper.selectList(null);
        sysPermissionInfos.sort(Comparator.comparing(SysPermissionInfo::getSort));
        PageInfo<SysPermissionInfo> pageInfo = new PageInfo<>(sysPermissionInfos);
        return BasisResultful.success(new TableListModel(pageInfo.getList(), pageInfo.getTotal()));
    }

    @Override
    public BasisResultful<Object> addPermission(SysPermissionInfoModel sysPermissionInfoModel) {
        if (null == sysPermissionInfoModel) {
            return BasisResultful.fail(BasisResultEnum.PARAM_IS_NULL);
        }
        boolean save = this.save(sysPermissionInfoModel);
        return save ? BasisResultful.success() : BasisResultful.fail();
    }

    @Override
    public BasisResultful<Object> updatePermission(SysPermissionInfoModel sysPermissionInfoModel) {
        if (null == sysPermissionInfoModel) {
            return BasisResultful.fail(BasisResultEnum.PARAM_IS_NULL);
        }
        this.saveOrUpdate(sysPermissionInfoModel);
        return BasisResultful.success();
    }

    @Override
    public BasisResultful<Object> deletePermission(SysPermissionInfoModel sysPermissionInfoModel) {
        if (null == sysPermissionInfoModel) {
            return BasisResultful.fail(BasisResultEnum.PARAM_IS_NULL);
        }
        QueryWrapper<SysPermissionInfo> wrapper = new QueryWrapper<>();
        wrapper.eq("permission_id", sysPermissionInfoModel.getPermissionId());
        int delete = sysPermissionInfoMapper.delete(wrapper);
        return delete > 0 ? BasisResultful.success() : BasisResultful.fail();
    }

    @Override
    public BasisResultful<Object> setPermissionStatus(Long permissionId, Integer status) {
        Integer integer = sysPermissionInfoMapper.updateStatus(permissionId, status);
        return integer > 0 ? BasisResultful.success() : BasisResultful.fail();
    }
}
